package utils;

import static play.libs.Json.toJson;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import play.Play;
import play.api.templates.Html;
import play.libs.WS;
import play.libs.F.Promise;
import akka.actor.UntypedActor;

import models.AppointmentHistory;
import models.Hospitals;
import models.User;


public class GCMSender  extends UntypedActor{

	public static void gcmSender(AppointmentHistory appointment ,
			User user,String message) {
		//User duser = User.getUserDetails(appointment.doctor.id);
		//System.out.println("test:"+duser.gcmRegistrationId);
		if(null !=user.gcmRegistrationId){
			GCMMessage sm = new GCMMessage();
			  sm.data = new Data();
			  sm.data.message=message;
			  sm.registration_ids.add(user.gcmRegistrationId);
			  String s = toJson(sm).toString();
			  Promise<WS.Response> result= WS.url("https://android.googleapis.com/gcm/send")
					  .setHeader("content-type","application/json")
			.setHeader("Authorization", "key=AIzaSyDXEeoqCDTNZn-IX3In1aPBvZBfns_zjzU")
			.post(s);
			  //System.out.println("status is :"+result.get().getBody()+ " "+ result.get().getStatus());
			
		}
	}
	

	@Override
	public void onReceive(Object message) throws Exception {
		
		if (message instanceof GCMCarrier){
			 System.out.println("calling actor1");
			gcmSender(((GCMCarrier) message).appointment,((GCMCarrier) message).user,((GCMCarrier) message).message);
		 }else
		      System.out.println("Error: Actor sent wrong message!!!");
		  
	}
	

	
	public static  class GCMMessage {

		public String collapse_key="new_appointment";
		public int time_to_live=108;
		public boolean  delay_while_idle=true;
		public Data data= new Data();
		public List<String> registration_ids=new ArrayList<String>();
	}
	
	public static class Data{
		public String message = null;
	} 

}
